package be.pluyckx.batteryprogress.components.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import be.pluyckx.batteryprogress.R;
import be.pluyckx.batteryprogress.data.Observer;
import be.pluyckx.batteryprogress.data.history.BatteryHistory;
import be.pluyckx.batteryprogress.data.history.BatteryLevel;

/**
 * Created by Philip on 31/07/13.
 */
public class BatteryListAdapter extends BaseAdapter implements Observer {
  private static final String TAG = "BatteryListAdapter";

  private final static Handler mThreadHandler = new Handler();
  private final LayoutInflater mInflater;
  private final Context mContext;
  private BatteryHistory mBatteryHistory;
  private int mDataSetObservers;

  public BatteryListAdapter(Context context) {
    mContext = context;
    mInflater = LayoutInflater.from(context);
    mBatteryHistory = BatteryHistory.getHistory();
    mDataSetObservers = 0;
  }

  public void destroy() {
    Log.d(TAG, "Remove observer");
    mBatteryHistory.removeObserver(this);
  }

  @Override
  public void registerDataSetObserver(DataSetObserver observer) {
    super.registerDataSetObserver(observer);

    mDataSetObservers++;
    Log.d(TAG, "DataSetObservers: " + mDataSetObservers);
    if (mDataSetObservers == 1) {
      Log.d(TAG, "Add observer");
      mBatteryHistory.addObserver(this);
    }
  }

  @Override
  public void unregisterDataSetObserver(DataSetObserver observer) {
    super.unregisterDataSetObserver(observer);
    mDataSetObservers--;
    Log.d(TAG, "DataSetObservers: " + mDataSetObservers);
    if (mDataSetObservers == 0) {
      Log.d(TAG, "Remove observer");
      mBatteryHistory.removeObserver(this);
    }
  }

  @Override
  public int getCount() {
    return (mBatteryHistory != null ? mBatteryHistory.size() : 0);
  }

  @Override
  public Object getItem(int i) {
    return (mBatteryHistory != null ? mBatteryHistory.get(mBatteryHistory.size() - i - 1) : null);
  }

  @Override
  public long getItemId(int i) {
    return mBatteryHistory.size() - i - 1;
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
      view = mInflater.inflate(R.layout.view_list_item, null, false);

      if (view != null) {
        ViewHolder holder = new ViewHolder();
        holder.tvDate = (TextView) view.findViewById(R.id.tv_date);
        holder.tvBatteryLevel = (TextView) view.findViewById(R.id.tv_battery_level);
        view.setTag(holder);
      }
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    BatteryLevel level = (BatteryLevel) getItem(i);

    if (level != null) {
      holder.tvDate.setText(level.getDateString());
      holder.tvBatteryLevel.setText(level.getBatteryLevelPercentageAsString());
      if (level.getState() == BatteryLevel.STATE_DISCHARGING) {
        view.setBackgroundResource(R.drawable.list_item_discharging);
      } else {
        view.setBackgroundResource(R.drawable.list_item_charging);
      }
    } else {
      holder.tvDate.setText(R.string.unknown);
      holder.tvBatteryLevel.setText(R.string.unknown);
    }

    return view;
  }

  @Override
  public void onDataChanged(Object observable, ChangeInfo datafield) {
    mThreadHandler.post(new Runnable() {
      @Override
      public void run() {
        notifyDataSetChanged();
      }
    });
  }

  private static class ViewHolder {
    TextView tvDate;
    TextView tvBatteryLevel;
  }
}
